package com.elephant.bean.selfPostProcessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.*;
import org.springframework.lang.Nullable;

import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;

/**
 * @author 林
 * @create 2025-05-22 15:47
 */
public class MyBeanPostProcessor implements BeanPostProcessor {

	private final ConfigurableApplicationContext applicationContext;

	public MyBeanPostProcessor(ConfigurableApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}
	@Override
	@Nullable
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		AccessControlContext acc = null;

		if (System.getSecurityManager() != null) {
			acc = this.applicationContext.getBeanFactory().getAccessControlContext();
		}
		invokeAwareInterfaces(bean);

		return bean;
	}

	//自定义的Aware接口放进去
	private void invokeAwareInterfaces(Object bean) {
		((ApplicationContextAware) bean).setApplicationContext(applicationContext);
	}
}
